package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author dxt
 * @create 2025-07-01-16:28
 */
@Component("1002")
public class TrackStrategy implements ItemTypeStrategy {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //判断用户是否购买过声音
        Long l = userPaidTrackMapper.selectCount(new QueryWrapper<UserPaidTrack>().eq("order_no", userPaidRecordVo.getOrderNo()));
        if (l > 0){
            return;
        }
        //获取需要添加声音的专辑Id
        TrackInfo trackInfo = albumFeignClient.getTrackInfo(userPaidRecordVo.getItemIdList().get(0)).getData();
        Long albumId = trackInfo.getAlbumId();
        //新增声音购买记录
        userPaidRecordVo.getItemIdList().stream().forEach(itemId -> {
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setTrackId(itemId);
            userPaidTrack.setAlbumId(albumId);
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrackMapper.insert(userPaidTrack);
        });
    }
}
